<?php

	class AppException extends Exception
	{
		public function __construct($message = '', $code = 0, Exception $previous = null)
		{
			if (in_array($code, array(
				System_Errors::ERROR_MODULE_NOT_FOUND,
				System_Errors::ERROR_SYSTEM_ONLY_MODE_CLI,
				System_Errors::ERROR_AUTH_NOT_PERMIT,
				System_Errors::ERROR_AUTH_DENIED,
				System_Errors::ERROR_AUTH_INVALIDATE,
			))
			) {
				parent::__construct($message, $code, $previous);

				return;
			}

			$this->message = $message;

			if (is_array($message) || is_object($message)){
				$message = json_encode($message);
			}

			$db = Database::getInstance();

			$db->insertTable('exception', array(
				'msg'      => $message,
				'msg_code' => $code,
				'trace'    => $this->getTraceAsString(),
				'request'  => http_build_query($_REQUEST),
				'date'     => date('Y-m-d H:i:s'),
			));
		}
	}